#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <arpa/telnet.h>
#include <signal.h>




/* best chatting program source code in ubuntu linux c */


void sighandler(int signum)
{

int clientsock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
/* best chatting program source code in ubuntu linux c */
servaddr.sin_addr.s_addr=inet_addr("46.108.33.138");/*Destination machine IP address*/
servaddr.sin_port=htons(5175);
/* best chatting program source code in ubuntu linux c */
char sendmsg[100];
memset(&sendmsg,0,strlen(sendmsg));
/* best chatting program source code in ubuntu linux c */
printf("\nYou:");
gets(sendmsg);
/* best chatting program source code in ubuntu linux c */
connect(clientsock,(struct sockaddr*)&servaddr,sizeof(servaddr));
send(clientsock,sendmsg,strlen(sendmsg),0);
printf("Message sent successfully!\n");
close(clientsock);
}


/* best chatting program source code in ubuntu linux c */


int main()
{
int servsock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
int connectedsock,len,recvdata;
struct sockaddr_in servaddr,clientaddr;
/* best chatting program source code in ubuntu linux c */
char recvmsg[100];
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(8080);
bind(servsock,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(servsock,1);
/* best chatting program source code in ubuntu linux c */
len=sizeof(clientaddr);

while(1)
{

signal(SIGINT, &sighandler);

connectedsock=accept(servsock,(struct sockaddr *) &clientaddr, (socklen_t*) &len);

/* best chatting program source code in ubuntu linux c */
memset(&recvmsg,0,strlen(recvmsg));
recvdata=recv(connectedsock,recvmsg,100,0);
recvmsg[recvdata]='\0';

/*PLEASE ADJUST THE FOLLWING LINE IF BREAKS*/
printf("%d.%d.%d.%d says:", clientaddr.sin_addr.s_addr&0xFF, (clientaddr.sin_addr.s_addr&0xFF00)>>8,(clientaddr.sin_addr.s_addr&0xFF0000)>>16,
(clientaddr.sin_addr.s_addr&0xFF000000)>>24);

/* best chatting program source code in ubuntu linux c */
puts(recvmsg);
}

close(connectedsock);
/* best chatting program source code in ubuntu linux c */
close(servsock);

return 0;


}


